/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.scene.shape.Shape.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class ProgressBarApp : GameApplication() {
	private var t = 0.0
	private lateinit var shape: Shape
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 650
		settings.height = 357
	}
	
	override fun initGame() {
		val rect1 = Rectangle(450.0, 45.0)
		rect1.translateX = 130.0
		rect1.translateY = 45.0
		
		val rect2 = Rectangle(20.0, 20.0)
		rect2.translateX = 460.0
		rect2.translateY = 90.0
		
		val rect2mid = Rectangle(30.0, 15.0)
		rect2mid.translateX = 500.0
		rect2mid.translateY = 90.0
		
		val rect3 = Rectangle(20.0, 20.0)
		rect3.translateX = 545.0
		rect3.translateY = 90.0
		
		val circle1 = Circle(70.0, 70.0, 70.0)
		val circle2 = Circle(70.0, 70.0, 40.0)
		
		shape = subtract(circle1, circle2)
		shape = union(shape, rect1)
		shape = union(shape, rect2)
		shape = union(shape, rect2mid)
		shape = union(shape, rect3)
		
		shape.strokeWidth = 4.5
		shape.stroke = Color.GRAY
		
		addUINode(shape, 30.0, 30.0)
	}
	
	override fun onUpdate(tpf: Double) {
		t += tpf
		
		if(t >= MAX) {
			t = 0.0
		}
		
		val offset = t / MAX
		
		val gradient = LinearGradient(0.0, 0.5, 1.0, 0.5, true, CycleMethod.NO_CYCLE,
			Stop(offset, Color.GREEN),
			Stop(1.0, Color.TRANSPARENT)
		)
		
		//shape.setStyle("-fx-fill: linear-gradient(from 0% 0% to 100% 100%, green " + (int)((offset*100)) +  "%, transparent " + (int)((1-offset)*100) + "%);");
		
		shape.style = "-fx-fill: linear-gradient(from 0% 50% to 100% 50%, green " + (offset * 100).toInt() + "%, transparent " + (offset * 100).toInt() + "%);"
		
		//shape.setFill(gradient);
	}
	
	companion object {
		private const val MAX = 3.0
	}
}


fun main(args: Array<String>) {
	GameApplication.launch(ProgressBarApp::class.java, args)
}

